
#include <iostream>
#include <audiopolicy.h>
#include <mmdeviceapi.h>
#include <string>
#include <Psapi.h>
#include <windows.h>
#include <stdio.h>
#include <tchar.h>
using namespace std;


void CHECK_HR(HRESULT h)
{

		cout << "hresult"<< (hex) << h << endl;
}

void PrintProcessNameAndID( DWORD processID )
{
    TCHAR szProcessName[MAX_PATH] = TEXT("<unknown>");

    // Get a handle to the process.

    HANDLE hProcess = OpenProcess( PROCESS_QUERY_INFORMATION |
                                   PROCESS_VM_READ,
                                   FALSE, processID );

    // Get the process name.

    if (NULL != hProcess )
    {
        HMODULE hMod;
        DWORD cbNeeded;

        if ( EnumProcessModules( hProcess, &hMod, sizeof(hMod), 
             &cbNeeded) )
        {
            GetModuleBaseName( hProcess, hMod, szProcessName, 
                               sizeof(szProcessName)/sizeof(TCHAR) );
        }
    }

    // Print the process name and identifier.

    _tprintf( TEXT("%s  (PID: %u)\n"), szProcessName, processID );

    CloseHandle( hProcess );
}


bool LPWToString(std::string& s, const LPWSTR pw, UINT codepage = CP_ACP)
{
    bool res = false;
    char* p = 0;
    int bsz;

    bsz = WideCharToMultiByte(codepage,
        0,
        pw,-1,
        0,0,
        0,0);
    if (bsz > 0) {
        p = new char[bsz];
        int rc = WideCharToMultiByte(codepage,
            0,
            pw,-1,
            p,bsz,
            0,0);
        if (rc != 0) {
            p[bsz-1] = 0;
            s = p;
            res = true;
        }
    }
    delete [] p;
    return res;
}


bool isRunning(string pName)
{
	unsigned long aProcesses[1024], cbNeeded, cProcesses;
	if(!EnumProcesses(aProcesses, sizeof(aProcesses), &cbNeeded))
		return false;

	cProcesses = cbNeeded / sizeof(unsigned long);
	for(unsigned int i = 0; i < cProcesses; i++)
	{
		if(aProcesses[i] == 0)
			continue;

		HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, 0, aProcesses[i]);
		char buffer[50];
		GetModuleBaseNameA(hProcess, 0, buffer, 50);
		CloseHandle(hProcess);
		if(pName == string(buffer))
			return true;
	}
	return false;
}

int GetNumberOfProcess()
{
	int hold = 0;
	HRESULT hr = S_OK;
	IMMDeviceEnumerator* deviceEnumerator = NULL;
	IMMDevice* device = NULL;
	LPWSTR deviceId = NULL;
	IAudioSessionManager2* sessionManager = NULL;
	IAudioSessionEnumerator* sessionEnumerator = NULL;
	int numberOfProcesses = 0;
	IAudioSessionControl* session = NULL;
	LPWSTR sessionName = NULL;
	string sessionNameStr;

	//create device enumerator
	
	CHECK_HR(hr = CoCreateInstance(__uuidof(MMDeviceEnumerator), NULL, CLSCTX_ALL, __uuidof(IMMDeviceEnumerator), (void**)&deviceEnumerator));
	// get default device
	CHECK_HR( hr = deviceEnumerator->GetDefaultAudioEndpoint(eRender, eConsole, &device));
	//CHECK_HR( hr =device->GetId(&deviceId));
	CHECK_HR( hr = device->Activate(__uuidof(IAudioSessionManager2), CLSCTX_ALL, NULL, (void**)&sessionManager));
	CHECK_HR( hr = sessionManager->GetSessionEnumerator(&sessionEnumerator));
	CHECK_HR( hr = sessionEnumerator ->GetCount(&numberOfProcesses));
	
	cout << "The number of processes using the default device is... " << numberOfProcesses << endl;

	return numberOfProcesses;

//	cin >> hold ;

}

int main()
{
	while(true)
	{
		if (isRunning("foobar2000.exe"))
		{
			cout <<"YES"<<endl;
		}
		LPVOID reserved = NULL;
		CoInitializeEx(reserved, COINIT_APARTMENTTHREADED);
		CoUninitialize();
		return 0;
	}
}
